package com.lal.sdk.proxy;

import com.lal.sdk.security.TokenManager;

import java.util.HashMap;
import java.util.Map;

public class StreamProxyManager {
    private final String proxyBaseUrl;
    private final TokenManager tokenManager;
    private final Map<String, StreamSession> activeSessions = new HashMap<>();
    
    public StreamProxyManager(String proxyBaseUrl) {
        this.proxyBaseUrl = proxyBaseUrl.replaceAll("/$", "");
        this.tokenManager = new TokenManager();
    }
    
    public StreamProxyManager(String proxyBaseUrl, long tokenValidityMs) {
        this.proxyBaseUrl = proxyBaseUrl.replaceAll("/$", "");
        this.tokenManager = new TokenManager(tokenValidityMs);
    }
    
    public String createSecureStreamUrl(String streamName, String clientIp, StreamFormat format) {
        // 创建认证token
        String token = tokenManager.createToken(streamName, clientIp);
        
        // 创建流会话
        StreamSession session = new StreamSession(streamName, clientIp, token, format);
        activeSessions.put(token, session);
        // 返回代理URL
        return String.format("%s/stream/%s/%s/%s/playlist.m3u8",
            proxyBaseUrl, format.toString().toLowerCase(), streamName, token);
    }
    
    public boolean validateStreamAccess(String token, String streamName, String clientIp) {
        return tokenManager.validateToken(token, streamName, clientIp);
    }
    
    public String getOriginalStreamUrl(String streamName, StreamFormat format) {
        switch (format) {
            case FLV:
                return "/live/" + streamName + ".flv";
            case HLS:
                return "/hls/" + streamName + "/playlist.m3u8";
            case WEBRTC:
                return "/webrtc/play?stream_name=" + streamName;
            default:
                throw new IllegalArgumentException("Unsupported format: " + format);
        }
    }
    
    public void revokeStreamAccess(String token) {
        activeSessions.remove(token);
        tokenManager.revokeToken(token);
    }
    
    public void cleanupExpiredSessions() {
        tokenManager.cleanupExpiredTokens();
        activeSessions.entrySet().removeIf(entry -> 
            !tokenManager.validateToken(entry.getKey(), 
                entry.getValue().getStreamName(), 
                entry.getValue().getClientIp())
        );
    }
    
    public enum StreamFormat {
        FLV, HLS, WEBRTC
    }
    
    private static class StreamSession {
        private final String streamName;
        private final String clientIp;
        private final String token;
        private final StreamFormat format;
        private final long createdTime;
        
        public StreamSession(String streamName, String clientIp, String token, StreamFormat format) {
            this.streamName = streamName;
            this.clientIp = clientIp;
            this.token = token;
            this.format = format;
            this.createdTime = System.currentTimeMillis();
        }
        
        public String getStreamName() { return streamName; }
        public String getClientIp() { return clientIp; }
        public String getToken() { return token; }
        public StreamFormat getFormat() { return format; }
        public long getCreatedTime() { return createdTime; }
    }
}